// -*- c-basic-offset: 2 default-tab-width: 2 indent-tabs-mode: t -*-
// vim: autoindent tabstop=2 noexpandtab shiftwidth=2 softtabstop=2
//===------- RJITCompiler.h - The Reactor just in time compiler -----------===//
//
//                            The VMKit project
//
// This file is distributed under the Purdue University Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef _RJITCOMPILER_H_
#define _RJITCOMPILER_H_

#include "llvm/CodeGen/GCMetadata.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JITEventListener.h"
#include "llvm/GVMaterializer.h"
#include "RCompiler.h"
#include "ReactorIntrinsics.h"
#include "Environment.h"

namespace R {

class RJITCompiler;

class RJITListener : public llvm::JITEventListener {
  RJITCompiler* TheCompiler;
public:
  RJITListener(RJITCompiler* Compiler) {
    TheCompiler = Compiler;
  }

  virtual void NotifyFunctionEmitted(
      const llvm::Function &F,
      void *Code,
      size_t Size,
      const llvm::JITEventListener::EmittedFunctionDetails &Details);
};


class RJITCompiler : public RCompiler, llvm::GVMaterializer {
	public:

		bool EmitFunctionName;
		RJITListener listener;
		llvm::ExecutionEngine* ee;
		llvm::GCModuleInfo* GCInfo;

		RJITCompiler();
		~RJITCompiler();  

		void            initPassManager();

		virtual bool MaterializeModule(llvm::Module *M, std::string *ErrInfo = 0);
		virtual bool isDematerializable(const llvm::GlobalValue *GV) const;
		virtual bool Materialize(llvm::GlobalValue *GV, std::string *ErrInfo = 0);
		virtual bool isMaterializable(const llvm::GlobalValue *GV) const;	

		static RJITCompiler* create();
		void jitCompile(FunInfo* info);
};
 
} // end namespace R

#endif
